using System;
using System.Data.Common;
using System.Data.Entity;
using System.Linq.Expressions;

namespace TSharp.Core.Repository
{
    public class RegStoreAttribute : TSharp.Core.Osgi.RegExtensionAttribute
    {
        public readonly string Description;
        private static Type storeType = typeof(IStorePart);
        private static readonly Type dbContextType = typeof(DbContext);
        public RegStoreAttribute(string description, Type storePart)
        {
            this.Description = description;
            StorePartType = storePart;
            IsDbContext = dbContextType.IsAssignableFrom(storePart);
        }

        public bool IsDbContext { get; private set; }


        public Type StorePartType { get; private set; }


        private Func<IStorePart> _new;
        public Func<IStorePart> New
        {
            get
            {
                if (_new == null)
                {
                    _new = Expression.Lambda<Func<IStorePart>>(Expression.New(StorePartType)).Compile();
                }
                return _new;
            }
        }
    }
}